<?php

namespace app\server\amqp\controller;


use app\common\model\Config;
use app\server\amqp\model\ConnectionUtil;
use app\server\notice\controller\Index;
use app\server\notice\model\NoticeLog;
use variant\library\vari\Log;

/**
 *
 * Class Sms
 * @package app\server\amqp\controller
 */
class Sms
{

    private $_host;
    private $_port;
    private $_virtualHost;
    private $_accessKey;
    private $_accessSecret;
    private $_resourceOwnerId;
    private $_queue = [];
    private $_connection;

    /**
     * Sms constructor.
     */
    public function __construct()
    {
        $this->_host = Config::get('host', 'amqp');
        $this->_port = Config::get('port', 'amqp');
        $this->_virtualHost = Config::get('virtual_host', 'amqp');
        $this->_accessKey = Config::get('access_key', 'amqp');
        $this->_accessSecret = Config::get('access_secret', 'amqp');
        $this->_resourceOwnerId = Config::get('resource_owner_id', 'amqp');
        $this->_queue = Config::get('queue', 'amqp');
    }

    public function run()
    {

        if (!isset($this->connection) || $this->connection->isConnected() === false) {
            echo 'sms/ems' . PHP_EOL;
            $this->_connection = new ConnectionUtil([
                $this->_host, $this->_port, $this->_virtualHost, $this->_accessKey, $this->_accessSecret, $this->_resourceOwnerId,
                'host' => $this->_host,
                'port' => $this->_port,
                'virtualHost' => $this->_virtualHost,
                'accessKey' => $this->_accessKey,
                'accessSecret' => $this->_accessSecret,
                'resourceOwnerId' => $this->_resourceOwnerId
            ]);
        }

    }

    /**
     * 短信/邮件
     *
     * @throws \ErrorException
     */
    public function index()
    {
        try {
            $this->run();
            $connection = $this->_connection->getConnection();
            $channel = $connection->channel();
            foreach ($this->_queue as $key => $value) {
                $channel->queue_declare($value, true, false, false, false);
            }
            $callback = function ($msg) {
                Log::instance()->notice('amqp: ' . $msg->body);
                $notice = new Index($msg->body);
                $log = new NoticeLog();
                if ($notice->check()) {
                    $id = $log->add(json_decode($msg->body, true));
                    $res = $notice->index();
                    $status = $res === true ? '1' : '-1';
                    $log->modify($id, $status, $res);
                }
                $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
            };

            foreach ($this->_queue as $key => $value) {
                $channel->basic_consume($value, '', false, true, false, false, $callback);
            }

            while (count($channel->callbacks)) {
                $this->run();
                $channel->wait();
            }
            $channel->close();
            $connection->close();
        } catch (\Exception $exception) {
            Log::instance()->warning('SMS:' . $exception->getMessage());
            $this->index();
        }

    }
}